package com.peng.leetcode.graph;

import java.util.*;

/**
 * CloneGraph
 *
 * @author: lupeng6
 * @create: 2021/1/6 18:49
 */
public class CloneGraph {

    public static void main(String[] args) {


    }


    static class Solution {
        private HashMap<Node, Node> visited = new HashMap<>();
        public Node cloneGraph(Node node) {
            if (node == null) {
                return null;
            }
            if (visited.containsKey(node)) {
                return visited.get(node);
            }
            Node nodeClone = new Node(node.val, new ArrayList<>());
            visited.put(node, nodeClone);
            for (Node neighbor : node.neighbors) {
                nodeClone.neighbors.add(cloneGraph(neighbor));
            }
            return nodeClone;
        }
    }

}
